package com.yungu.swift.system.finance.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.system.finance.dao.FinanceAccountSettlementMapper;
import com.yungu.swift.system.finance.model.dto.FinanceAccountSettlementDto;
import com.yungu.swift.system.finance.model.param.AdminAccountPageParam;
import com.yungu.swift.system.finance.model.param.AdminAccountSettlementPageParam;
import com.yungu.swift.system.finance.service.FinanceAccountSettlementService;
import com.yungu.swift.system.sys.dao.SysAccountAgentMapper;
import com.yungu.swift.system.sys.dao.SysAccountCompanyMapper;
import com.yungu.swift.system.sys.model.dto.SysAccountAgentDto;
import com.yungu.swift.system.sys.model.dto.SysAccountCompanyDto;
import com.yungu.swift.system.sys.model.vo.SysAgentAccountVo;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 账户结算
 */
@Service
public class FinanceAccountSettlementServiceImpl implements FinanceAccountSettlementService {

    @Autowired
    private FinanceAccountSettlementMapper financeAccountSettlementMapper;
    @Autowired
    private SysAccountAgentMapper sysAccountAgentMapper;
    @Autowired
    private SysAccountCompanyMapper sysAccountCompanyMapper;

    /**
     * 结算
     * @param uuid
     * @return
     */
    @Transactional
    @Override
    public ResponseData<Boolean> settlementHandle(String uuid) {
        FinanceAccountSettlementDto financeAccountSettlementDto = financeAccountSettlementMapper.get(uuid);
        if(financeAccountSettlementDto==null){
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE,"数据不存在");
        }
        if(financeAccountSettlementDto.getSettlementStatus()==1){
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE,"该月份已结算");
        }
        String accountUuid = financeAccountSettlementDto.getAccountUuid();
        //可结算金额
        BigDecimal settlementAmount = financeAccountSettlementDto.getSettlementAmount();
        //代理
        if(financeAccountSettlementDto.getAccountType()==1){
            SysAccountAgentDto sysAccountAgentDto = sysAccountAgentMapper.list(MapUtils.build("uuid",accountUuid)).get(0);
            SysAccountAgentDto updAccount = new SysAccountAgentDto();
            updAccount.setUuid(sysAccountAgentDto.getUuid());
            //减去可提现金额
            updAccount.setCashBalance(sysAccountAgentDto.getCashBalance().subtract(settlementAmount));
            //累积结算金额
            updAccount.setSettlementAccumulate(ParamUtil.defaultIfNull(sysAccountAgentDto.getSettlementAccumulate(),BigDecimal.ZERO).add(settlementAmount));
            sysAccountAgentMapper.edit(updAccount);
        }else{
            //公司
            SysAccountCompanyDto sysAccountCompanyDto = sysAccountCompanyMapper.list(MapUtils.build("uuid",accountUuid)).get(0);
            SysAccountCompanyDto updAccount = new SysAccountCompanyDto();
            updAccount.setUuid(sysAccountCompanyDto.getUuid());
            //减去可提现金额
            updAccount.setCashBalance(sysAccountCompanyDto.getCashBalance().subtract(financeAccountSettlementDto.getSettlementAmount()));
            //累积结算金额
            updAccount.setSettlementAccumulate(ParamUtil.defaultIfNull(sysAccountCompanyDto.getSettlementAccumulate(),BigDecimal.ZERO).add(settlementAmount));
            sysAccountCompanyMapper.edit(updAccount);
        }
        financeAccountSettlementMapper.settlementHandle(uuid);
        return ResponseData.buildSuccessResponse("结算成功",true);
    }

    @Override
    public ResponseData<PageVo<FinanceAccountSettlementDto>> queryPageVo(AdminAccountSettlementPageParam accountPageParam) {
        PageBounds pageBounds = new PageBounds(accountPageParam.getPageNum(), accountPageParam.getPageSize());
        PageList<FinanceAccountSettlementDto> pageList = financeAccountSettlementMapper.accountSettlementPage(accountPageParam, pageBounds);
        PageVo<FinanceAccountSettlementDto> pageVo = new PageVo(accountPageParam.getPageNum(), accountPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    /**
     * 统计
     * @param accountUuid
     * @return
     */
    @Override
    public ResponseData<Map<String, Object>> statistics(String accountUuid) {
        return ResponseData.buildSuccessResponse(financeAccountSettlementMapper.statistics(accountUuid));
    }
}
